﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

using MSBTC.WinFormHelpers;

namespace MSBTC.WinForms
{
    public partial class DBConnForm : Form
    {
        FormHelpers helper = new FormHelpers();
        SessionHelper seshelp = new SessionHelper();
        public Form newform;

        public DBConnForm()
        {
            InitializeComponent();
        }

        private void DBConnectionManager_Load(object sender, EventArgs e)
        {
            this.txtPass.PasswordChar = '\u25CF';
            if ((File.Exists(helper.ConfigXML) == true) && (helper.Read_Configuration() == true) && (helper.error != "ERROR"))
            {                
                MessageBox.Show("Credential already exist, you can now Connect to Login page", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtip.Text = helper.DBAddress;
                txtPort.Text = helper.DBPort;
                txtPass.Text = helper.DBPassword;
                txtUID.Text = helper.DBUsername;
                //txtip.Enabled = false;
                //txtPort.Enabled = false;
                //txtPass.Enabled = false;
                //txtUID.Enabled = false;
                btnconn.Enabled = true;
                btnrest.Enabled = false;
                btntest.Enabled = true;     
            }
            else
            {
                btntest.Enabled = true;
                btnconn.Enabled = false;
                btnrest.Enabled = false;
            }
        }

        private void btntest_Click(object sender, EventArgs e)
        {
            if (helper.DBConnect(txtip.Text.Trim(), txtPort.Text, txtUID.Text, txtPass.Text) == true)
            {
                MessageBox.Show("Successfully Tested, You may now connect to Login", "Connection Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnconn.Enabled = true;
            }
            else
            {
                MessageBox.Show(helper.ex + " Try to Reset the configuration", "Connection Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnrest.Enabled = true;
                btntest.Enabled = false;
                return;
            }
        }

        private void btnconn_Click(object sender, EventArgs e)
        {
            if (helper.Save_Configuration() == true)
            {
                Login LForm = new Login();
                LForm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show(helper.ex, "Connection Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnrest_Click(object sender, EventArgs e)
        {

            if (helper.ResetConfig() == true)
            {
                MessageBox.Show("Configuration has been reset, Please fill up the needed information", "Connection Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btntest.Enabled = true;
                btnrest.Enabled = false;
                return;
            }
            else
            {
                MessageBox.Show(helper.ex + "System is Terminated", "Connection Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }

    }
}